package gui.components.JXTable.JXRow;

import gui.components.JXTable.JXTableModel.JXTableModel;
import gui.components.JXTable.JXCell.iJXCellRenderer;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Vector;

/**
 * Date: 06.09.2005
 * Time: 11:10:34
 */
public class JXRowSet implements iJXRowSet{

    private JXTableModel dtm;
    private JTable table = null;
    private JList rowHeader = new JList();
    private Vector rowHeadertext = new Vector();
    private JScrollPane scrollPane = null;
    //private JXCellRenderer renderer;
    private JXRowHeaderRenderer headerRenderer;
    private ArrayList<iJXCellRenderer> renderers = new ArrayList<iJXCellRenderer>();
    public JXRowSet(final JTable table, JXRowHeaderRenderer headerRenderer, JXTableModel dtm, JScrollPane scrollPane, ArrayList<iJXCellRenderer> renderers){
        this.headerRenderer = headerRenderer;
        this.renderers = renderers;
        this.table = table;
        this.dtm = dtm;
        this.scrollPane = scrollPane;
        this.rowHeader.setFixedCellWidth(50);
        rowHeader.setFixedCellHeight(table.getRowHeight()+3);
        rowHeader.setCellRenderer(new JXRowHeaderRenderer(table));
        this.scrollPane.setRowHeaderView(rowHeader);
        this.scrollPane.getRowHeader().getView().setBackground(table.getTableHeader().getBackground());
    }
    public JXRowHeaderRenderer getHeaderRenderer(){
        return headerRenderer;
    }
    public ArrayList<iJXCellRenderer> getCellRenderer(){
        return renderers;
    }
    protected Vector getRowHeaderText(){
        return rowHeadertext;
    }

    public void setNumerable(boolean b){
        if (b)this.scrollPane.setRowHeaderView(rowHeader);
        else this.scrollPane.setRowHeaderView(null);
    }

    protected void removeNumber(){
        rowHeadertext.removeElement(rowHeadertext.lastElement());
    }

    public JList getDefaultRowHeader(){
        return rowHeader;
    }

    public JXTableModel getTableModel(){
        return dtm;
    }

    public JTable getTable(){
        return table;
    }

    public Integer addRow(Object ob, Object[] strings) {
        Integer pos = dtm.getRowCount();
        int len = strings.length+1;
        Object[] objt = new Object[len];
        objt[0] = ob;
        for(int i = 1;i<len;i++){
            objt[i] = strings[i-1];
        }
        dtm.addRow(objt);
        rowHeadertext.add(String.valueOf(pos));
        rowHeader.setListData(rowHeadertext);
        System.out.print(".");
        return pos;
    }

    protected Integer transformKeyToPosition(Object key){
        for(int i=0;i<dtm.getRowCount();i++){
            if(dtm.getValueAt(i,0).equals(key)) return i;
        }
        return null;
    }

    protected Object transformPositionToKey(Integer pos){
        return dtm.getValueAt(pos,0);
    }

    public JXRow getRowFromKey(Object key) {
        Integer pos = this.transformKeyToPosition(key);
        return new JXRow(this,pos);
    }

    public JXRow getRowFromPos(Integer pos) {
        return new JXRow(this,pos);
    }

    public ArrayList<JXRow> getRowsFromObject(Object ob) {
        ArrayList<JXRow> jxRows = new ArrayList<JXRow>();
        for(int i=0;i<dtm.getRowCount();i++){
            if(dtm.getValueAt(i,0).equals(ob)) jxRows.add(new JXRow(this,i));
        }
        return jxRows;
    }

    public JXRow getRowFromObjectFirst(Object ob) {
        for(int i=0;i<dtm.getRowCount();i++){
            if(dtm.getValueAt(i,0).equals(ob)) return (new JXRow(this,i));
        }
        return null;
    }

    public JXRow getRowFromObjectLast(Object ob) {
        for(int i=(dtm.getRowCount()-1);i>=0;i--){
            if(dtm.getValueAt(i,0).equals(ob)) return (new JXRow(this,i));
        }
        return null;
    }

    public JXRow getSelectedRow() {
        Integer pos = table.getSelectedRow();
        return new JXRow(this,pos);
    }
    public ArrayList<JXRow> getSelectedRows() {
        int[] pos = table.getSelectedRows();
        ArrayList<JXRow> res = new ArrayList<JXRow>();
        for (int k : pos)
            res.add(new JXRow(this,k));
        return res;
    }

    public JXRowHeader getRowHeaderFromKey(Integer key) {
        Integer pos = this.transformKeyToPosition(key);
        return new JXRowHeader(this,pos);
    }

    public JXRowHeader getRowHeaderFromPos(Integer pos) {
        return new JXRowHeader(this,pos);
    }

    public JXRowHeader getSelectedRowHeader() {
        Integer pos = table.getSelectedRow();
        return new JXRowHeader(this,pos);
    }

    public int getRowCount() {
        return table.getRowCount();
    }

    public void addMarkRow(int row, Color color) {
        for(int i=0;i<renderers.size();i++)
        renderers.get(i).addColorRow(row, color);
    }

    public void removeAllMarkRow() {
        for(int i=0;i<renderers.size();i++)
        renderers.get(i).removeAllColorRow();
    }

    public void removeMarkRow(int row) {
        for(int i=0;i<renderers.size();i++)
        renderers.get(i).removeColorRow(row);
    }

    public void setSelectRow(Integer pos) {
        this.table.clearSelection();
        this.table.addRowSelectionInterval(pos,pos);
        int rowHeight = this.table.getRowHeight();
        this.table.scrollRectToVisible(new Rectangle(0,rowHeight*pos,this.table.getWidth(),rowHeight));
        //table.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "selectNextColumnCell");
        //this.table.requestFocusInWindow();
        this.table.requestFocusInWindow();
    }

}
